<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>The ajax page</title>
</head>
<body>
    <div>
        <h1>The ajax page</h1>
        <button onclick="doAjaxGet()">Get</button>
        <button onclick="doAjaxPost()">Post</button>
        <button onclick="doAjaxDelete()">Delete</button>
        <button onclick="doAjaxPut()">Put</button>
    </div>
    <div id="result"></div>
    <form enctype="application/x-www-form-urlencoded"></form>
    <script>
        function doAjaxGet() {
            // 1、创建ajax对象
            let xhr = new XMLHttpRequest();
            // 2、添加ajax对象的状态监听时间
            xhr.onreadystatechange = function() {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            // 3、打开与服务端的连接
            xhr.open("GET", "/doAjaxGet", true);
            // 4、发送数据
            xhr.send();
        }

        function doAjaxPost() {
            let params = "id=2&name=李四&gender=男&job=xxx";
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            xhr.open("POST", "/doAjaxPost", true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send(params);
        }

        function doAjaxDelete() {
            // 1、创建ajax对象
            let xhr = new XMLHttpRequest();
            // 2、添加ajax对象的状态监听时间
            xhr.onreadystatechange = function() {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            // 3、打开与服务端的连接
            xhr.open("DELETE", "/doAjaxDelete?id=1", true);
            // 4、发送数据
            xhr.send();
        }

        function doAjaxPut() {
            let params = "id=1&name=zhangsan&gender=nv&job=c#";
            // 1、创建ajax对象
            let xhr = new XMLHttpRequest();
            // 2、添加ajax对象的状态监听时间
            xhr.onreadystatechange = function() {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            // 3、打开与服务端的连接
            xhr.open("PUT", "/doAjaxPut", true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            // 4、发送数据
            xhr.send(params);
        }
    </script>
</body>
</html>